package com.icity.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.icity.common.Result;
import com.icity.entity.Application;
import com.icity.entity.ArticleStatistical;
import com.icity.entity.EquipmentDistribution;
import com.icity.entity.EquipmentSummary;
import com.icity.entity.PartnerMsg;
import com.icity.entity.PermissionsEntity;
import com.icity.entity.ProductType;
import com.icity.entity.RegisteredUse;
import com.icity.entity.ResourceDistribution;
import com.icity.entity.ScreenAccess;
import com.icity.entity.ScreenTraffic;
import com.icity.entity.StatisticalSummary;
import com.icity.service.EquipmentSummaryService;
import com.icity.service.StatisticalSummaryService;
import com.icity.util.DateUtils;
import com.icity.util.PermissionsUtil;
//设备概括
@RestController
@RequestMapping(value="/admin/EquipmentSummary")
public class EquipmentSummaryController {
  @Autowired
  private EquipmentSummaryService equipmentSummaryService;
  /**
   * 获取设备概括的三个icon
   * @return
   */
  @RequestMapping(value="/getEquimentSummaryCount")
 // @PostMapping(value="/getCount")
  @ResponseBody
  public Result getCount(HttpServletRequest request) {
	  Result result=new Result();
	 try {		
		 //获取合伙人id
		 String partner_id=request.getParameter("partner_id");
		 Map<Object, Object> map=new HashMap<>();
		 map.put(partner_id, "partner_id");
		 List<EquipmentSummary> list = equipmentSummaryService.getAllEquimentSummary(map);;
			if(list != null && list.size() > 0) {
				result.setMessage("获取成功");
				result.setCode("1");
				result.setData(list);
			}else {
				result.setCode("-1");
				result.setData(new ArrayList<>());
				result.setMessage("暂无数据");
			}
	} catch (Exception e) {
		result=new Result();
		result.setCode("-1");
		result.setMessage("获取数据异常");
	}
	  
	  return result;
  }
  
  
  /**
   * 产品类型概况   & 数据详情接口
   * 
   */
  @RequestMapping(value="/getProductTypeOverview")
  // @PostMapping(value="/getCount"
   @ResponseBody
   public Result getProductTypeOverview(HttpServletRequest request) {
		  Result result=new Result();
		 try {		
			 //获取合伙人id
			 String partner_id=request.getParameter("partner_id");
			 Map<Object, Object> map=new HashMap<>();
			 map.put(partner_id, "partner_id");
			 List<ProductType> list = equipmentSummaryService.getProductTypeOverview(map);;
				if(list != null && list.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(list);
				}else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}
		} catch (Exception e) {
			result=new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}
		  
		  return result;
	  }
  /**
   * 大屏分析接口（七个icon）
   *
   */
  @RequestMapping(value="/getAllScreenAnalysis")
  // @PostMapping(value="/getCount"
   @ResponseBody
   public Result getAllScreenAnalysis(HttpServletRequest request) {
		  Result result=new Result();
		 try {		
			 //获取合伙人id
			 String partner_id=request.getParameter("partner_id");
			//获取应用唯一标识
			 String resources_package_unique_code=request.getParameter("resources_package_unique_code");
			 Map<Object, Object> map=new HashMap<>();
			 map.put(partner_id, "partner_id");
			 map.put(resources_package_unique_code, "resources_package_unique_code");
			 List<ScreenAccess> lists =null;
			 List<ScreenAccess> list = equipmentSummaryService.getAllScreenAnalysis(map);
			 
			    int resources_package_journal_num=0;
				int resources_package_book_num=0;
				int application_announce_ment_num=0;
				int application_article_num=0;
				int application_video_num=0;
				int application_bananer_num=0;
				int application_equipment=0;

				for(int i=0;i<list.size();i++) {
					ScreenAccess screenAccess=list.get(i) ;
					resources_package_journal_num=resources_package_journal_num+screenAccess.getResources_package_journal_num();
					resources_package_book_num=resources_package_book_num+screenAccess.getResources_package_book_num();
					application_announce_ment_num=application_announce_ment_num+screenAccess.getApplication_announce_ment_num();
					application_article_num=application_article_num+screenAccess.getApplication_article_num();
					application_video_num=application_video_num+screenAccess.getApplication_video_num();
					application_bananer_num=application_bananer_num+screenAccess.getApplication_bananer_num();
					application_equipment=application_equipment+screenAccess.getApplication_equipment();
				}
				ScreenAccess screenAccess=new ScreenAccess();
				screenAccess.setResources_package_journal_num(resources_package_journal_num);
				screenAccess.setResources_package_book_num(resources_package_book_num);
				screenAccess.setApplication_announce_ment_num(application_announce_ment_num);
				screenAccess.setApplication_article_num(application_article_num);
				screenAccess.setApplication_video_num(application_video_num);
				screenAccess.setApplication_bananer_num(application_bananer_num);
				screenAccess.setApplication_equipment(application_equipment);
				lists.add(screenAccess);
			 
			 
				if(list != null && lists.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(lists);
				}else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}
		} catch (Exception e) {
			result=new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}
		  
		  return result;
	  }
  
  
  /**
   * 大屏分析访问量概况+访问趋势（饼状图+趋势图）
   *
   */
  @RequestMapping(value="/getAllScreenAnalysisPie")
  // @PostMapping(value="/getCount"
   @ResponseBody
   public Result getAllScreenAnalysisPie(HttpServletRequest request) {
		  Result result=new Result();
		 try {		
			 //获取合伙人id
			 String partner_id=request.getParameter("partner_id");
			//获取应用唯一标识
			 String resources_package_unique_code=request.getParameter("resources_package_unique_code");
			 //获取参数map
			 Map <String,String[]> properties= request.getParameterMap();
			 Map<Object, Object> map =new HashMap<>();
			 for(Iterator iter = properties.keySet().iterator();iter.hasNext();){
				           String name = (String)iter.next();
				            String[] values = (String[]) properties.get(name);
				            String valueStr = "";
				             for(int i = 0 ; i <values.length;i++){
				 
				                valueStr = (i == values.length -1)?valueStr + values[i]:valueStr + values[i]+",";
				             }
				            //把数据全部加进map集合中
				             map.put(name,valueStr);
				         } 			
						
			 map.put(partner_id, "partner_id");
			 map.put(resources_package_unique_code, "resources_package_unique_code");			 			  			 
			  List<ScreenTraffic> lists =null;
			    int journal_traffic=0;
				int book_traffic=0;
				int announce_ment_traffic=0;
				int article_traffic=0;
				int video_traffic=0;
				int bananer_traffic=0;
				int audio_traffic=0;
               List<ScreenTraffic> list=equipmentSummaryService.getAllScreenAnalysisPie(map);
          
              
				for(int i=0;i<list.size();i++) {
					ScreenTraffic screenTraffic=list.get(i) ;
					journal_traffic=journal_traffic+screenTraffic.getJournal_traffic();
					book_traffic=book_traffic+screenTraffic.getBook_traffic();
					announce_ment_traffic=announce_ment_traffic+screenTraffic.getAnnounce_ment_traffic();
					article_traffic=article_traffic+screenTraffic.getArticle_traffic();
					video_traffic=video_traffic+screenTraffic.getVideo_traffic();
					bananer_traffic=bananer_traffic+screenTraffic.getBananer_traffic();
					audio_traffic=audio_traffic+screenTraffic.getAudio_traffic();
				}
				ScreenTraffic screenTraffic=new ScreenTraffic();
				screenTraffic.setJournal_traffic(journal_traffic);
				screenTraffic.setBook_traffic(book_traffic);
				screenTraffic.setAnnounce_ment_traffic(announce_ment_traffic);
				screenTraffic.setArticle_traffic(article_traffic);
				screenTraffic.setVideo_traffic(video_traffic);
				screenTraffic.setBananer_traffic(bananer_traffic);
				screenTraffic.setAudio_traffic(audio_traffic);
				lists.add(screenTraffic);
				
			 
			 
				if(list != null && list.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(lists);
					
				}else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					
					result.setMessage("暂无数据");
				}
		} catch (Exception e) {
			result=new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			
		}
		  
		  return result;
	  }
  
 // public static void main(String[] args) {
	
	/*  Map<String,String[]> requestgetParameterMap = new HashMap();  
      requestgetParameterMap.put("1", new String[] { "qweqwe" });  
      requestgetParameterMap.put("2", new String[] { "qwe", "qweqwe" });  
      requestgetParameterMap.put("3", new String[] { "qw2131e", "qweqwe" ,"2323"});
      requestgetParameterMap.put("4", new String[] { " "});
      Map<Object, Object> map =new HashMap<>();*/
		/* for(Iterator iter = requestgetParameterMap.keySet().iterator();iter.hasNext();){
			           String name = (String)iter.next();
			            String[] values = (String[]) requestgetParameterMap.get(name);
			            String valueStr = "";
			             for(int i = 0 ; i <values.length;i++){
			 
			                valueStr = (i == values.length -1)?valueStr + values[i]:valueStr + values[i]+",";
			             }
			            //把数据全部加进map集合中
			             map.put(name,valueStr);
			             System.out.println(map);
			         } 	*/	
      
    /*  for(Entry<String, String[]> entrys:requestgetParameterMap.entrySet())
      {
    	  Object valueObj = entrys.getValue();
          String value = ""; 
      if(StringUtils.isBlank(String.valueOf(valueObj))){ value = ""; }
      else if(valueObj instanceof String[]){ String[] values = (String[])valueObj;
      for(int i=0;i<values.length;i++){ value += values[i] + ","; } 
      value = value.substring(0, value.length()-1); }else{ value = valueObj.toString(); } 
      map.put(entrys.getKey(), value); }
     
      System.out.println(map);*/
//}
}
